package vn.edu.uit.chemistry.utilities;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Map;

import vn.edu.uit.chemistry.model.ThongTinFileBaiCu;
import vn.edu.uit.chemistry.protocol.Message;
import vn.edu.uit.chemistry.protocol.ThongTinCaNhan;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.util.Log;

public class ClientThreadReveiver implements Runnable {

	private Socket socket;
	private Thread thread;

	int fileSize = 30 * 1024;
	int bytesRead;
	int current = 0;
	String path = Environment.getExternalStorageDirectory().getAbsolutePath()
			.toString()
			+ "/hoa hoc";
	public static int percent = 0;
	String host = "";

	Context ctx;

	public ClientThreadReveiver(Socket _sk, String _host, Context _ctx)
			throws IOException {
		System.out.println("Client connected");
		this.socket = _sk;
		this.ctx = _ctx;
		this.host = _host;
	}

	public void start() {

		thread = new Thread(this);
		thread.start();
		while (!thread.isAlive()) {
			//
		}

	}

	public void stop() throws IOException {

		if (socket != null) {
			socket.shutdownInput();
			socket.shutdownOutput();
			socket.close();
		}
	}

	public void run() {
		try {
			receiveFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void receiveFile() throws IOException {

		String pos = "bat ky";
		String mshs = "";
		String tenHS = "";
		String nameFile = "";
		int total = 0;

		SharedPreferences prefs = ctx.getSharedPreferences("Path", 0);
		pos = prefs.getString("pos", "bat ky");
		if (pos.equals("bai cu")) {
			path += "/kiem tra/bai cu/";
			prefs = ctx.getSharedPreferences("DS_NOPBAICU", 0);
			String[] values = prefs.getString(host, "mshs#tenHS#nameFile#0")
					.split("#");

			mshs = values[0];
			tenHS = values[1];
			nameFile = values[2];
			total = Integer.parseInt(values[3]);
		} else if (pos.equals("bat ky") || pos.equals("tai lieu")) {
			path += "/" + pos + "/";
			prefs = ctx.getSharedPreferences("FILENHAN", 0);
			tenHS = prefs.getString("sender", "");
			nameFile = prefs.getString("name", "");
			total = Integer.parseInt(prefs.getString("size", ""));
		}

		fileSize = total + 1024;
		byte[] myByteArray = new byte[fileSize];
		InputStream is = socket.getInputStream();
		bytesRead = is.read(myByteArray, 0, myByteArray.length);
		current = bytesRead;

		do {
			bytesRead = is.read(myByteArray, current, myByteArray.length
					- current);
			if (bytesRead >= 0) {
				current += bytesRead;
				percent = current * 100 / total;
				if (pos.equals("bai cu")) {
					SharedPreferences ref = ctx.getSharedPreferences(
							"PTNOPBAI", 0);
					SharedPreferences.Editor editor = ref.edit();
					editor.putString(mshs, tenHS + "#" + percent);
					editor.commit();
				} else if (pos.equals("bat ky") || pos.equals("tai lieu")) {
					prefs = ctx.getSharedPreferences("FILENHAN", 0);
					SharedPreferences.Editor editor = prefs.edit();
					editor.putInt("percent", percent);
					editor.commit();
				}

			}

		} while (bytesRead > -1);

		FileOutputStream fos = new FileOutputStream(path + nameFile);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		bos.write(myByteArray, 0, current);
		bos.flush();
		bos.close();

		stop();
	}

}
